package com.service.people.imp;

import java.util.List;

import org.apache.log4j.Logger;

import com.AContext;
import com.bo.People;
import com.bo.SearchFilter;
import com.manager.people.IPeopleManager;
import com.service.people.IPeopleService;
import com.util.StringUtil;

public class PeopleService implements IPeopleService{
	
	private static final Logger LOG = Logger.getLogger(PeopleService.class);

	private AContext context;
	
	private IPeopleManager peopleManager;
	
	public PeopleService(AContext context){
		this.context = context;
	}

	public void addPeople(People people) throws Exception {
		peopleManager.add(people);
	}

	public void deletePeople(String realName) throws Exception {
		People people = findByName(realName);
		peopleManager.delete(people);
	}
	
	@Override
	public void deletePeople(int id) throws Exception {
		People people = peopleManager.findById(id);
		peopleManager.delete(people);
	}

	public People findByName(String realName) throws Exception {
		if(StringUtil.isEmpty(realName)){
			return null;
		}
		return peopleManager.findByName(realName);
	}
	
	@Override
	public People findById(String id) throws Exception {
		if(id == null){
			return null;
		}
		People people = peopleManager.findById(Integer.parseInt(id));
		return people;
	}

	public boolean isExist(String realName) {
		People people = null;
		try {
			 people = findByName(realName);
		} catch (Exception e) {
			LOG.error("find people failed !", e);
		}
		if(people != null){
			return true;
		}
		return false;
	}

	public void start() {
		this.peopleManager = context.getPeopleManager();		
	}

	public void stop() {
		LOG.info("people service is stopped !");
	}

	public void uptatePeople(People people) throws Exception {
		if(people == null){
			LOG.error("people is null , so not to update !");
			return ;
		}
		peopleManager.update(people);
	}

	@Override
	public List<People> getPeopleList(People people) {
		return peopleManager.getPeopleList(people);
	}

	@Override
	public int getLoserStaticsticsData(SearchFilter sf) throws Exception {
		sf.addEqCondition("type", 1); 
		return peopleManager.listPeople(sf).size();
	}
}
